import java.awt.Color;
import java.awt.Graphics;
import java.util.Map;

import com.speljohan.rsbot.bot.Bot;
import com.speljohan.rsbot.event.events.ServerMessageEvent;
import com.speljohan.rsbot.event.listeners.PaintListener;
import com.speljohan.rsbot.event.listeners.ServerMessageListener;
import com.speljohan.rsbot.script.Constants;
import com.speljohan.rsbot.script.Script;
import com.speljohan.rsbot.script.wrappers.RSInterface;
import com.speljohan.rsbot.script.wrappers.RSNPC;
import com.speljohan.rsbot.script.wrappers.RSObject;
import com.speljohan.rsbot.script.wrappers.RSTile;

public class OakBUYER extends Script implements PaintListener,
		ServerMessageListener { // Names the script

	public long startTime = System.currentTimeMillis();

	public long lastCheck;
	public long checkTime;

	public int money = 995;
	int loads = 0;

	public static final int[] sawmill = new int[] { 4250 };
	public static final int[] bank = new int[] { 11402 };
	public static final int[] wood = new int[] { 8778, 1521 };
	public static final int[] banker = new int[] { 5913, 5912 };
	public static final int[] rock = new int[] { 19143 };

	RSTile[] buyingpath = { new RSTile(3253, 3421), new RSTile(3260, 3428),
			new RSTile(3264, 3428), new RSTile(3265, 3428),
			new RSTile(3277, 3439), new RSTile(3282, 3447),
			new RSTile(3284, 3454), new RSTile(3286, 3461),
			new RSTile(3290, 3468), new RSTile(3295, 3479),
			new RSTile(3298, 3482), new RSTile(3302, 3490) };

	RSTile[] lumbridgepath = { new RSTile(3230, 3219), new RSTile(3237, 3224),
			new RSTile(3247, 3226), new RSTile(3260, 3234),
			new RSTile(3258, 3243), new RSTile(3253, 3251),
			new RSTile(3249, 3266), new RSTile(3245, 3274),
			new RSTile(3238, 3283), new RSTile(3239, 3293),
			new RSTile(3238, 3304), new RSTile(3241, 3309),
			new RSTile(3246, 3315), new RSTile(3252, 3321),
			new RSTile(3259, 3325), new RSTile(3269, 3331),
			new RSTile(3277, 3338), new RSTile(3282, 3344),
			new RSTile(3290, 3348), new RSTile(3296, 3355),
			new RSTile(3298, 3364), new RSTile(3298, 3364),
			new RSTile(3299, 3371), new RSTile(3296, 3378),
			new RSTile(3293, 3386), new RSTile(3292, 3393),
			new RSTile(3293, 3403), new RSTile(3291, 3411),
			new RSTile(3288, 3417), new RSTile(3280, 3424),
			new RSTile(3273, 3428), new RSTile(3266, 3427),
			new RSTile(3255, 3428), new RSTile(3253, 3420) };

	RSTile[] bankpath = reversePath(buyingpath);

	// stole anti-ban from
	int clickRandomBankSpotbyTile() {
		final int GambleInt = random(1, 2);

		switch (GambleInt) {

		case 1:
			final RSTile y = new RSTile(3252, 3418);
			atTile(y, "Bank Banker");
			break;

		case 2:
			final RSTile g = new RSTile(3254, 3418);
			atTile(g, "Bank Banker");
			break;
		}

		return random(500, 1000);
	}

	@Override
	public String getAuthor() { // Sets the Authors name
		return "Hellfish";
	}

	@Override
	public String getName() { // Sets the Scripts name
		return "Oak buyer";
	}

	@Override
	public String getScriptCategory() { // Sets the category that the script
		// fit's into
		return "Money"; // ex Woodcutting, Fishing, Money Making, Quests
	}

	@Override
	public String getScriptDescription() { // Shows a description of the script
		// inside the script dump area
		return "";
	}

	@Override
	public double getVersion() { // Sets the Version number of the script
		return 1.0;
	}

	@Override
	public int loop() {
		if (getMyPlayer().isMoving()) {
			return random(0, 5);
		}
		final RSObject near = findObject(OakBUYER.rock);
		if (near != null && distanceTo(near.getLocation()) <= 18) {
			setCameraRotation(359);
		} else {
			setCameraRotation(181);
		}

		if (inventoryContains(1521)) {
			walkPathMM(randomizePath(buyingpath, 2, 2), 15);
			wait(random(1, 2));
			final RSNPC n = getNearestFreeNPCByID(OakBUYER.sawmill);
			if (n != null && distanceTo(n.getLocation()) <= 9) {
				final RSTile l = new RSTile(3302, 3492);
				atTile(l, "ank");
				if (RSInterface.getInterface(403).isValid()) {
					final int x = random(310, 342);
					final int y = random(93, 103);
					clickMouse(x, y, false);
					wait(random(100, 150));
					atMenu("All");
					setRun(true);
					wait(random(8, 13));
				}
			}
		} else {
			walkPathMM(randomizePath(bankpath, 2, 2), 15);
			wait(random(500, 1500));
			final RSObject b = findObject(OakBUYER.bank);
			if (b != null) {
				clickRandomBankSpotbyTile();
				if (RSInterface.getInterface(Constants.INTERFACE_BANK)
						.isValid()
						&& inventoryContains(995)) {
					atInventoryItem(8778, "all");
					wait(random(529, 1260));
					final int o = random(50, 51);
					final int p = random(98, 108);
					clickMouse(o, p, false);
					wait(random(300, 500));
					final int e = random(55, 56);
					final int f = random(218, 219);
					clickMouse(e, f, true);
					final int k = random(489, 490);
					final int c = random(34, 35);
					clickMouse(k, c, true);
					loads++;
				}

			}

		}

		return random(100, 150);
	}

	@Override
	public void onFinish() { // Loads any commands to do when the script is
		// ended
		Bot.getEventManager().removeListener(PaintListener.class, this);
		Bot.getEventManager().removeListener(ServerMessageListener.class, this);
	}

	public void onRepaint(final Graphics g) {
		if (isLoggedIn()) {

			long millis = System.currentTimeMillis() - startTime;
			final long hours = millis / (1000 * 60 * 60);
			millis -= hours * 1000 * 60 * 60;
			final long minutes = millis / (1000 * 60);
			millis -= minutes * 1000 * 60;
			final long seconds = millis / 1000;

			g.setColor(Color.BLUE);
			g.drawString("Loads of planks bought:" + loads, 371, 30);
			g.drawString("run: " + hours + " hrs " + minutes + " min "
					+ seconds + " sec.", 371, 15);
		}
	}

	@Override
	public boolean onStart(final Map<String, String> args) { // Loads any
		// commands
		// needed when the
		// script is started
		return true;
	}

	public void serverMessageRecieved(final ServerMessageEvent e) {
		if (e.getMessage().contains("trade")) {
			log("someone wishes to trade!");
			wait(random(1000, 4000));
		}
	}
}